﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Search_BasicSearch : System.Web.UI.Page
{
    //Need to add the Provenance Variant Name and Item Parties
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["query"] != null)
        {
            txtSearchValue.Text = Request.QueryString["query"];
            SearchData();
        }

    }

    private void BasicSearchProvenance(string usersrch)
    {
        using (PublicCatalogDataContext db = new PublicCatalogDataContext())
        {

            var myProvenance = from Provenance in db.Provenances
                               where Provenance.ProvenanceName.Contains(@usersrch)
                               | Provenance.ProvenanceAdministrativeHistory.Contains(@usersrch)
                               select new
                               {
                                   Provenance.ProvenanceName,
                                   Provenance.ProvenanceId
                               };
            lblProvenanceHeader.Visible = true;
            lblNoProvenance.Visible = true;
            lblNoProvenance.Text = "No matches found for Provenance";
            foreach (var q in myProvenance)
            {
                lblProvenanceHeader.Visible = true;
                if (q.ProvenanceName != "")
                {
                    lblNoProvenance.Visible = false;
                    provListView.DataSource = myProvenance;
                    provListView.DataBind();
                }
                if (q.ProvenanceName == "")
                {
                    lblNoProvenance.Visible = true;
                    lblNoProvenance.Text = "No matches found for Provenance";
                }
                if (q.ProvenanceName == null)
                {
                    lblNoProvenance.Visible = true;
                    lblNoProvenance.Text = "No matches found for Provenance";
                }
            }
        }
    }

    private void BasicSearchSeries(string usersrch)
    {
        using (PublicCatalogDataContext dc = new PublicCatalogDataContext())
            {

            var mySeries = from Series in dc.Series
                               where Series.SeriesTitle.Contains(@usersrch)
                               | Series.SeriesDescription.Contains(@usersrch)
                               select new
                               {
                                   Series.SeriesTitle,
                                   Series.SeriesId
                               };
            lblSeriesHeader.Visible = true;
            lblNoSeries.Visible = true;
            lblNoSeries.Text = "No matches found for Series";
            foreach (var p in mySeries)
            {
                lblSeriesHeader.Visible = true;
                if (p.SeriesTitle != "")
                {
                    lblNoSeries.Visible = false;
                    seriesListView.DataSource = mySeries;
                    seriesListView.DataBind();
                }
            }
        }
    }

    private void BasicSearchItems(string usersrch)
    {
        using (PublicCatalogDataContext de = new PublicCatalogDataContext())
            {

                var myItems = from Items in de.Items
                               where Items.ItemTitle.Contains(@usersrch)
                               | Items.ItemDisplayTitle.Contains(@usersrch) 
                               | Items.ItemDescription.Contains(@usersrch)
                               //|| Items.ItemParties.Contains(@usersrch)
                               select new
                               {
                                   Items.ItemTitle,
                                   Items.ItemId
                               };
                lblItemHeader.Visible = true;
                lblNoItem.Visible = true;
                lblNoItem.Text = "No matches found for Item";
                foreach (var r in myItems)
                {
                    lblItemHeader.Visible = true;
                    if (r.ItemTitle != "")
                    {
                        lblNoItem.Visible = false;
                        itmListView.DataSource = myItems;
                        itmListView.DataBind();
                    }
                }
            }
    }

    
    private void SearchData()
    {
        lblProvenanceHeader.Visible = false;
        lblNoProvenance.Visible = false;
        lblNoProvenance.Text = "No matches found for Provenance";
        lblSeriesHeader.Visible = false;
        lblNoSeries.Visible = false;
        lblNoSeries.Text = "No matches found for Series";
        lblItemHeader.Visible = false;
        lblNoItem.Visible = false;
        lblNoItem.Text = "No matches found for Item";
        //rptProvenanceResults.Visible = false;
        //rptSeriesResults.Visible = false;
        //rptItemResults.Visible = false;

        string usersrch = txtSearchValue.Text.ToString();
        BasicSearchProvenance(usersrch);
        BasicSearchSeries(usersrch);
        BasicSearchItems(usersrch);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SearchData();
        }
        else
        {
            Response.Redirect("BasicSearch.aspx");
        }

    }
    protected void btnDownload_Click()
    {
    }

    protected override void OnPreRender(EventArgs e)
    {
        itmListView.DataBind();
        base.OnPreRender(e);
    }
}
